<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="餐饮公司" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['canygsId', validatorRules.canygsId]" :trigger-change="true" dictCode="canygs" placeholder="请选择餐饮公司"/>
        </a-form-item>
        <a-form-item label="销售额" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['xiaose', validatorRules.xiaose]" placeholder="请输入销售额" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="原材料" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['yuancl', validatorRules.yuancl]" placeholder="请输入原材料" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="人员工资" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['renygz', validatorRules.renygz]" placeholder="请输入人员工资" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="水电气" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['shuidq', validatorRules.shuidq]" placeholder="请输入水电气" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="总计" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['zongj', validatorRules.zongj]" placeholder="请输入总计" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="原材料成本率(%)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['yuanclcbl', validatorRules.yuanclcbl]" placeholder="请输入原材料成本率(%)" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="净利润 (%)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['jinglr', validatorRules.jinglr]" placeholder="请输入净利润 (%)" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="利润率 (%)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['lirl', validatorRules.lirl]" placeholder="请输入利润率 (%)" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="统计日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择统计日期" v-decorator="['tongrq', validatorRules.tongrq]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"


  export default {
    name: "HqStCbhsModal",
    components: { 
      JDate,
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          canygsId: {
            rules: [
              { required: true, message: '请输入餐饮公司!'},
            ]
          },
          xiaose: {
            rules: [
              { required: true, message: '请输入销售额!'},
            ]
          },
          yuancl: {
            rules: [
              { required: true, message: '请输入原材料!'},
            ]
          },
          renygz: {
            rules: [
              { required: true, message: '请输入人员工资!'},
            ]
          },
          shuidq: {
            rules: [
              { required: true, message: '请输入水电气!'},
            ]
          },
          zongj: {
            rules: [
              { required: true, message: '请输入总计!'},
            ]
          },
          yuanclcbl: {
            rules: [
              { required: true, message: '请输入原材料成本率(%)!'},
            ]
          },
          jinglr: {
            rules: [
              { required: true, message: '请输入净利润 (%)!'},
            ]
          },
          lirl: {
            rules: [
              { required: true, message: '请输入利润率 (%)!'},
            ]
          },
          tongrq: {
            rules: [
              { required: true, message: '请输入统计日期!'},
            ]
          },
        },
        url: {
          add: "/stCbhs/hqStCbhs/add",
          edit: "/stCbhs/hqStCbhs/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'canygsId','xiaose','yuancl','renygz','shuidq','zongj','yuanclcbl','jinglr','lirl','tongrq'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'canygsId','xiaose','yuancl','renygz','shuidq','zongj','yuanclcbl','jinglr','lirl','tongrq'))
      },

      
    }
  }
</script>